package com.video.web.controller.system;

import java.util.List;

import com.video.common.utils.MinioAPI;
import com.video.system.domain.VideoCategory;
import com.video.system.domain.VideoTeacher;
import com.video.system.service.IVideoCategoryService;
import com.video.system.service.IVideoTeacherService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.video.common.annotation.Log;
import com.video.common.enums.BusinessType;
import com.video.system.domain.VideoCourse;
import com.video.system.service.IVideoCourseService;
import com.video.common.core.controller.BaseController;
import com.video.common.core.domain.AjaxResult;
import com.video.common.utils.poi.ExcelUtil;
import com.video.common.core.page.TableDataInfo;

/**
 * 课程Controller
 * 
 * @author lyh
 * @date 2021-07-20
 */
@Controller
@RequestMapping("/system/course")
public class VideoCourseController extends BaseController
{
    private String prefix = "system/course";

    @Autowired
    private IVideoCourseService videoCourseService;
    @Autowired
    private IVideoCategoryService videoCategoryService;
    @Autowired
    private IVideoTeacherService videoTeacherService;
    @Autowired
    private MinioAPI minioAPI;

    /**
     * 获取上传视频的URL
     * @return
     * @throws Exception
     */
    @PostMapping("/show")
    @ResponseBody
    public AjaxResult showUrl(String fileName) throws Exception {
        //桶
        String bucket="test";

        // 获取文件签名url
        String signedUrl = minioAPI.getSignedUrl(bucket, fileName, 300);
        // 该路径可访问文件五分钟
        System.out.println("=========================>signedUrl:"+signedUrl);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("url", signedUrl);//存入上传路径
        return ajax;
    }

    @RequiresPermissions("system:course:view")
    @GetMapping()
    public String course()
    {
        return prefix + "/course";
    }

    /**
     * 查询课程列表
     */
    @RequiresPermissions("system:course:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VideoCourse videoCourse)
    {
        startPage();
        List<VideoCourse> list = videoCourseService.selectVideoCourseList(videoCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程列表
     */
    @RequiresPermissions("system:course:export")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VideoCourse videoCourse)
    {
        List<VideoCourse> list = videoCourseService.selectVideoCourseList(videoCourse);
        ExcelUtil<VideoCourse> util = new ExcelUtil<VideoCourse>(VideoCourse.class);
        return util.exportExcel(list, "课程数据");
    }

    /**
     * 新增课程
     */
    @GetMapping("/add")
    public String add(ModelMap map)
    {
        List<VideoCategory> videoCategories = videoCategoryService.selectCodeAndNameList();
        List<VideoTeacher> videoTeachers = videoTeacherService.selectCodeAndNameList();
        map.put("all1",videoTeachers);
        map.put("all",videoCategories);

        return prefix + "/add";
    }

    /**
     * 新增保存课程
     */
    @RequiresPermissions("system:course:add")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VideoCourse videoCourse)
    {
        return toAjax(videoCourseService.insertVideoCourse(videoCourse));
    }

    /**
     * 修改课程
     */
    @GetMapping("/edit/{cId}")
    public String edit(@PathVariable("cId") Integer cId, ModelMap mmap)
    {
        VideoCourse videoCourse = videoCourseService.selectVideoCourseById(cId);
        mmap.put("videoCourse", videoCourse);

        List<VideoCategory> videoCategories = videoCategoryService.selectCodeAndNameList();
        List<VideoTeacher> videoTeachers = videoTeacherService.selectCodeAndNameList();
        mmap.put("all",videoCategories);
        mmap.put("all1",videoTeachers);

        return prefix + "/edit";
    }

    /**
     * 修改保存课程
     */
    @RequiresPermissions("system:course:edit")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VideoCourse videoCourse)
    {
        return toAjax(videoCourseService.updateVideoCourse(videoCourse));
    }

    /**
     * 删除课程
     */
    @RequiresPermissions("system:course:remove")
    @Log(title = "课程", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(videoCourseService.deleteVideoCourseByIds(ids));
    }
}
